package com.zcx.service.impl;

import com.zcx.bean.FileModel;
import com.zcx.dao.FileMapper;
import com.zcx.service.FileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

@Service
public class FileServiceImpl implements FileService {

    @Resource
    private FileMapper fileMapper;

    public boolean saveFileToPath(MultipartFile file1, String path) {

        try {
            //创建文件
            File file = new File(path);
            if(!file.exists()){
                file.mkdirs();
            }
            //复制到file1
            file1.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean saveFileToMysql(FileModel fileModel1) {

        return fileMapper.saveFile(fileModel1)>0;
    }

    public boolean deleteFileByPath(String path) {
        File file = new File(path);
        return file.delete();
    }

    public String getPathByUUID(String uuid) {
        return fileMapper.getInfoByUUID(uuid).getUrl();
    }

    public FileModel getInfoByUUID(String uuid) {
        return fileMapper.getInfoByUUID(uuid);
    }
}
